/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.image.imagemagick;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.process.ProcessExecutor;
import org.im4java.process.ProcessStarter;

/**
 *
 * @author anagnosg
 */
public class ImageMagickUtils {
    public static String IMAGE_SEARCH_MAGICK_PATH ="/usr/bin/convert:/usr/bin/exiftool"  ;
    public static boolean init = false;
    public static void initPaths(){
        if(!init){
        //String myPath="C:\\Programs\\ImageMagick;C:\\Programs\\exiftool";
            ProcessStarter.setGlobalSearchPath(IMAGE_SEARCH_MAGICK_PATH); 
            init=true;
        }
    }
    
    public static void initPaths(String imageMagicPath){
        
            //String myPath="C:\\Programs\\ImageMagick;C:\\Programs\\exiftool";
            ProcessStarter.setGlobalSearchPath(imageMagicPath); 
            init=true;
        
    }
      
    
    public static void imageMagickCommand(ParameterList params,String imageSource , String imageDest)throws IOException, InterruptedException, IM4JavaException{
        initPaths();
        ProcessExecutor exec = new ProcessExecutor();
        
        System.out.println("imageSource"+imageSource+"imageDest"+imageDest);
        ConvertCmd cmd = new ConvertCmd();
        // create the operation, add images and operators/options
        IMOperation op = new IMOperation();
        //String imagePath = "/home/anagnosg/tmp/imagesTests/images/";
        op.addImage(imageSource);
        //op.resize(width,height);
        
        
        op.addImage(imageDest);
        // execute the operation
        cmd.run(op);
        exec.shutdown();
        if (exec.awaitTermination(30,TimeUnit.SECONDS)) {
          //System.err.println("processes terminated on their own");
        } else {
          System.err.println("trying to cancel all running processes ...");
          exec.shutdownNow();
        }
    }
    
    public static void resizeSingle(int width,int height,String imageSource,String imageDest) throws IOException, InterruptedException, IM4JavaException{
        initPaths();
        ProcessExecutor exec = new ProcessExecutor();
        
        System.out.println("imageSource"+imageSource+"imageDest"+imageDest);
        ConvertCmd cmd = new ConvertCmd();
        // create the operation, add images and operators/options
        IMOperation op = new IMOperation();
        //String imagePath = "/home/anagnosg/tmp/imagesTests/images/";
        op.addImage(imageSource);
        op.resize(width,height);
        
        
        op.addImage(imageDest);
        // execute the operation
        cmd.run(op);
        exec.shutdown();
        if (exec.awaitTermination(30,TimeUnit.SECONDS)) {
          //System.err.println("processes terminated on their own");
        } else {
          System.err.println("trying to cancel all running processes ...");
          exec.shutdownNow();
        }
        //Thread.sleep(1000);
    }
}
